///////////////////////////////////////////////////////////////
// File: "CTallyState.h"
// Author: James Gonzalez (JG)
// Purpose: To contain all Main Menu functionality (play, options, credits, how to, exit)
///////////////////////////////////////////////////////////////


#include "CTallyState.h"
#include "../Wrappers/CSGD_DirectInput.h"
//#include "CGamePlayState.h"
#include "CBitmapFontEngine.h"
#include "CGame.h"
#include "CWiimote.h"
//#include "CAnimationManager.h"
#include "CLevelSelectState.h"
#include "CProfileSelectState.h"
#include "CTransitionScreen.h"
#include "CGamePlayState.h"

#include "CHighScoresState.h"
#include "CProfileManager.h"
#include "CProfile.h"
#include "CSoundManager.h"



CTallyState::CTallyState()
{

	m_fTallyTimer = 0;
	m_fTallyUpTimer = 0;
	SetupTallyScreen(0,0,0,0,0,0,0,0);
	m_fTimeTally = 0;
	m_nScoreTally= 0;
	m_nBombsUsedTally= 0;
	m_nEnemiesKilledTally= 0;
	m_nTimesDiedTally= 0;
	m_nJumpsTally= 0;
	m_nNumRotationsTally= 0;
	m_nPowerUpsUsedTally= 0;
	m_nKeysTally = 0;

	///Transition members/////////////////////////////////////////////////////
	m_bExitingState = false;
	//////////////////////////////////////////////////////////////////////////
	m_bLevelTen = false;

	m_bTime_Tallied = false;
	m_bScore_Tallied= false;
	m_bBombsUsed_Tallied= false;
	m_bEnemiesKilled_Tallied= false;
	m_bTimesDied_Tallied= false;
	m_bJumps_Tallied= false;
	m_bNumRotations_Tallied= false;
	m_bPowerUpsUsed_Tallied= false;
	m_bKeys_Tallied= false;

}

CTallyState::~CTallyState()
{

}

void CTallyState::Enter()
{
	//SetupTallyScreen(300,300,3,5,2,55,4,6,2);
	CTransitionScreen::GetInstance()->OpenDoor();
	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BitmapFonts/BS_TechDemoTitle.xml");
	//CAnimationManager::GetInstance()->AnimationPlayer("Walking",true);

	///Screen Data//////////////////////////////////////////////////////////////////////////
	m_cScreen.LoadScreen("Screens/BS_TallyScreen.xml");
	TopX =m_cScreen.GetPosObjMembers("Top",XPOS);
	TopY = m_cScreen.GetPosObjMembers("Top",YPOS);
	LeftX =m_cScreen.GetPosObjMembers("Left",XPOS);
	LeftY = m_cScreen.GetPosObjMembers("Left",YPOS);
	RightX =m_cScreen.GetPosObjMembers("Right",XPOS);
	RightY = m_cScreen.GetPosObjMembers("Right",YPOS);
	////////////////////////////////////////////////////////////////////////////////////////

	m_bExitingState = false;
	m_fTimeTally = 0;
	m_nScoreTally= 0;
	m_nBombsUsedTally= 0;
	m_nEnemiesKilledTally= 0;
	m_nTimesDiedTally= 0;
	m_nJumpsTally= 0;
	m_nNumRotationsTally= 0;
	m_nFinalScore = 0;
	m_nPowerUpsUsedTally= 0;

	m_bTime_Tallied = false;
	m_bScore_Tallied= false;
	m_bBombsUsed_Tallied= false;
	m_bEnemiesKilled_Tallied= false;
	m_bTimesDied_Tallied= false;
	m_bJumps_Tallied= false;
	m_bNumRotations_Tallied= false;
	m_bPowerUpsUsed_Tallied= false;
	m_bKeys_Tallied= false;

	m_fTallyTimer = 0;

	m_nTallyScreen = FIRST;

	m_nFinalScore += (int)(m_fTime * 3);
	m_nFinalScore -= (m_nBombsUsed * 25) ;
	m_nFinalScore += (m_nEnemiesKilled * 100);
	m_nFinalScore -= (m_nTimesDied * 25) ;
 	switch(CProfileManager::GetInstance()->GetCurrentProfile()->GetCurrentPwrUp())
 	{
	case CProfile::BALLOON:
		m_nFinalScore -= (int)(m_fTime-45) * 3;
		break;
	case CProfile::BRICK :
		m_nFinalScore -= (int)(m_fTime-15) * 3;
		break;
	case CProfile::GOO : 
		m_nFinalScore -= (int)(m_fTime-60) * 3;
		break;
	case CProfile::METAL_BOOTS:
		m_nFinalScore -= (int)(m_fTime-45) * 3;
		break;
	case CProfile::MIRROR :
		m_nFinalScore -= (int)(m_fTime-25) * 3;
		break;
	case CProfile::PARACHUTE :
		m_nFinalScore -= (int)(m_fTime-25) * 3;
		break;
	case CProfile::SPRING :
		m_nFinalScore -= (int)(m_fTime-30) * 3;
		break;
 	}


	if (m_nFinalScore < 0)
	{
		m_nFinalScore = 0;
	}

	m_nPrevScore = CProfileManager::GetInstance()->GetCurrentProfile()->GetScore();

	
	m_bLevelTen = false;

}

void CTallyState::Exit()
{
	m_cScreen.ExitScreen();	
	if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(),"Level/BS_LEVEL3n.xml") && CGamePlayState::GetInstance()->GetIsChallenge() && m_nNumRotationsTally <= 2)
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 3);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL1n.xml") && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 1);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL2x.xml") && CGamePlayState::GetInstance()->GetL2Challenge() && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 2);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL4n.xml") && CGamePlayState::GetInstance()->GetL4Challenge() && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 4);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL5n.xml") && CGamePlayState::GetInstance()->GetL5Challenge() && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 5);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL6n.xml") && CGamePlayState::GetInstance()->GetKeyGain() == 2 && CGamePlayState::GetInstance()->GetKeyUse() == 2 && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 6);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL9x.xml") && CGamePlayState::GetInstance()->GetL9Challenge() && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 9);
	}
	else if (0 == strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(),"Level/BS_LEVEL8n.xml") && CGamePlayState::GetInstance()->GetEnemiesKilled() >= 3 )
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false,false,true,8);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL10h.xml") && CGamePlayState::GetInstance()->GetEnemyKill() >= 6 && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 10);
	}
	else if(0 ==strcmp(CGamePlayState::GetInstance()->GetCurrentLevelName(), "Level/BS_LEVEL7x.xml") && CGamePlayState::GetInstance()->GetL7Challenge() && CGamePlayState::GetInstance()->GetIsChallenge())
	{
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, false, true, 7);
	}
	CProfileManager::GetInstance()->GetCurrentProfile()->SetScore( m_nPrevScore + m_nFinalScore);

	CGamePlayState::GetInstance()->SetIsChallenge(false);

	if(m_nTimesDied == 0)
		CProfileManager::GetInstance()->GetCurrentProfile()->AddTrophy(false, true, false);
}

bool CTallyState::Input(float fElapsedTime)
{

	CWiimote* pWii = CWiimote::GetInstance();
	//wiiuse_poll(pWii->GetWiimote(), MAX_WII_REMOTES);
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if (CProfileManager::GetInstance()->GetCurrentProfile()->GetWhichLevelPlaying() == 10 && !m_bLevelTen)
	{
		CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_HIGH_SCORE);		
		m_bLevelTen = true;
	}

	if (pWii->IsButtonJustPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE))
	{
		m_bExitingState = true;
	}

	if (pWii->IsButtonJustPressed(WII_A) || pDI->KeyPressed(DIK_RETURN))
	{
		if (m_nTallyScreen == FIRST )
		{
			m_nTallyScreen = SECOND;
			m_fTallyTimer = 0;
		}
		else if (m_nTallyScreen == SECOND )
		{
			m_nTallyScreen = THIRD;
			m_fTallyTimer = 0;
		}
		else if (m_nTallyScreen == THIRD )
		{
			m_nTallyScreen = DONE;
			m_fTallyTimer = 0;
		}
		else if (m_nTallyScreen == DONE)
		{
			m_bExitingState = true; 
			m_fTallyTimer = 0;
		}
	}

	///Close door then change state///////////////////////////////////////////
	if (m_bExitingState)
	{
		//Go to High Scores or back to level select
		if (m_bLevelTen)
		{
			CHighScoresState::GetInstance()->IsEndOfGame(true);
			CTransitionScreen::GetInstance()->ChangeTo(CHighScoresState::GetInstance());
		}
		else
			CTransitionScreen::GetInstance()->ChangeTo(CLevelSelectState::GetInstance());
	}
	//////////////////////////////////////////////////////////////////////////

	return true;
}

void CTallyState::Update(float fElapsedTime)
{
	//CAnimationManager::GetInstance()->UpdateAnimation("Walking",fElapsedTime);
	static bool once = true;
	//Game time
	if (m_nTallyScreen == FIRST)
	{
		once = true;
		if (!m_bTime_Tallied)
		{
			m_bTime_Tallied = TallyUpFloats(&m_fTimeTally,m_fTime,fElapsedTime); 
		}
		else if (m_bTime_Tallied && !m_bBombsUsed_Tallied)
		{
			m_bBombsUsed_Tallied = TallyUpInts(&m_nBombsUsedTally,m_nBombsUsed,fElapsedTime); 
		}
		else if (m_bBombsUsed_Tallied && !m_bPowerUpsUsed_Tallied)
		{
			m_bPowerUpsUsed_Tallied = TallyUpInts(&m_nPowerUpsUsedTally,m_nPowerUpsUsed,fElapsedTime); 
		} 
		else if (m_bPowerUpsUsed_Tallied && !m_bKeys_Tallied)
		{
			m_bEnemiesKilled_Tallied = TallyUpInts(&m_nEnemiesKilledTally,m_nEnemiesKilled,fElapsedTime); 
		}

		if(m_bKeys_Tallied)
		{
			m_fTallyTimer += fElapsedTime;
			if (m_fTallyTimer >= 5)
			{
				m_fTallyTimer = 0;
				m_nTallyScreen = SECOND; 
			}
		}

	}

	if (m_nTallyScreen == SECOND)
	{

		if (!m_bTimesDied_Tallied)
		{
			m_bTimesDied_Tallied = TallyUpInts(&m_nTimesDiedTally,m_nTimesDied,fElapsedTime); 
		}
		else if (m_bTimesDied_Tallied && !m_bJumps_Tallied)
		{
			m_bJumps_Tallied = TallyUpInts(&m_nJumpsTally,m_nJumps,fElapsedTime); 
		}
		else if (m_bJumps_Tallied && !m_bNumRotations_Tallied)
		{
			m_bNumRotations_Tallied = TallyUpInts(&m_nNumRotationsTally,m_nNumRotations,fElapsedTime); 
		}
		else if (m_bNumRotations_Tallied && !m_bEnemiesKilled_Tallied)
		{
			m_bEnemiesKilled_Tallied = TallyUpInts(&m_nEnemiesKilledTally,m_nEnemiesKilled,fElapsedTime); 
		}

		if (m_bEnemiesKilled_Tallied)
		{		
			m_fTallyTimer += fElapsedTime;
			if (m_fTallyTimer >= 5)
			{
				m_fTallyTimer = 0;
				m_nTallyScreen = THIRD;
		
			}
			
		}
	}

	else if (m_nTallyScreen == THIRD)
	{
		m_fTallyTimer+= fElapsedTime;
		m_bScore_Tallied = TallyUpInts(&m_nScoreTally,m_nFinalScore,fElapsedTime); 
		
		if (m_fTallyTimer >= 5)
		{	
			m_fTallyTimer = 0;
			m_nTallyScreen = DONE;
		}
	}

	if (m_nTallyScreen == DONE && once)
	{
		m_nScoreTally = m_nFinalScore;
	}



}

void CTallyState::Render()
{
	m_cScreen.RenderScreen();


	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();


	pBFE->RenderString("Tally Screen", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), TopX, TopY+25 , 0);

	char buff[128];


	if (m_nTallyScreen == FIRST )
	{

		//Game Time
		pBFE->RenderString("Time Remaining:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY,0);
		sprintf_s(buff,"%.2f",m_fTimeTally); 
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+32,0);
		pBFE->RenderString("x 3",m_nFontID,0,0xFF00FF00,RightX+20,RightY+32,0);

		//Number of Bombs Used
		pBFE->RenderString("Bombs Used:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+64,0);
		sprintf_s(buff,"%d",m_nBombsUsedTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+96,0);
		pBFE->RenderString("x -50",m_nFontID,0,0xFFFF0000,RightX+20,RightY+96,0);

		//Number of Power Ups
		pBFE->RenderString("PowerUps Used:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+128,0);
		sprintf_s(buff,"%d",m_nPowerUpsUsedTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+160,0);
		//pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,RightX+20,RightY+175,0);

		//Enemies
		pBFE->RenderString("Enemies Killed:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+192,0);
		sprintf_s(buff,"%d",m_nEnemiesKilledTally); 
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+224,0);
		pBFE->RenderString("x 100",m_nFontID,0,0xFF00FF00,RightX+20,RightY+224,0);
	}
	else if (m_nTallyScreen == SECOND)
	{
	
		//Died
		pBFE->RenderString("Times Died:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY,0);
		sprintf_s(buff,"%d",m_nTimesDiedTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+32,0);
		pBFE->RenderString("x -25",m_nFontID,0,0xFFff0000,RightX+20,RightY+32,0);

		//Jumps
		pBFE->RenderString("# of Jumps:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+64,0);
		sprintf_s(buff,"%d",m_nJumpsTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+96,0);
		//pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,RightX+20,RightY+475,0);

		//Number of Rotations
		pBFE->RenderString("# of Rotation:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+128,0);
		sprintf_s(buff,"%d",m_nNumRotationsTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+160,0);
		//pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,RightX+20,RightY+175,0);

		//Keys picked up
		pBFE->RenderString("Keys Picked Up:",m_nFontID,0,0xFFFFFFFF,LeftX,LeftY+192,0);
		sprintf_s(buff,"%d",m_nKeysTally); 
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,LeftX+20,LeftY+224,0);
		//pBFE->RenderString("x 100",m_nFontID,0,0xFFFFFF00,RightX+20,RightY+224,0);
	}
	
	else if (m_nTallyScreen == THIRD || m_nTallyScreen == DONE)
	{
		//Score
		pBFE->RenderString("Final Score:",m_nFontID,0,0xFFFF0000,200,200,0);
		sprintf_s(buff,"%d",m_nScoreTally);
		pBFE->RenderString(buff,m_nFontID,0,0xFFFFFF00,250,232,0);
		
	}








}

bool CTallyState::TallyUpFloats(float * fDest, float fFinal ,float fElapsedTime)
{
	static float mTimer = 0;
	mTimer += fElapsedTime;
	if (*fDest < fFinal)
	{
		if ( mTimer >= 0.001f)
		{
			CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_TALLY);
			mTimer = 0;
			*fDest += 1.01f; 
		}
	}
	else
	{
		*fDest = fFinal;
		return true;
	}

	return false;

}
bool CTallyState::TallyUpInts(int * nDest, int nFinal ,float fElapsedTime )
{
	static float mTimer = 0;
	mTimer += fElapsedTime;
	if (*nDest < nFinal  )
	{
		if (mTimer >= 0.001f)
		{
			CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_TALLY);
			mTimer = 0;
			*nDest+= 1; 
		}

	}
	else
	{

		*nDest = nFinal;
		return true;
	}
	return false;
}


void CTallyState::SetupTallyScreen(	float fTime,int nBombsUsed,int nEnemiesKilled,int nTimesDied,int nJumps,int nNumRotations,int nPowerUpsUsed, int nKeys)
{
	m_fTime			=	fTime ;
	m_nBombsUsed	= 	nBombsUsed  ;
	m_nPowerUpsUsed = 	nPowerUpsUsed ;

	m_nEnemiesKilled=	nEnemiesKilled ;
	m_nTimesDied	= 	nTimesDied  ;
	m_nJumps		= 	nJumps  ;

	m_nNumRotations = 	nNumRotations ;
	m_nKeys			=	nKeys;


}
